<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;
use think\Db;
use think\Controller;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //状态码 0成功 100没有携带参数  101请传入用户uid 400失败 
    private $code = [0,100,101,400,102];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
    //任务相关接口
    //添加任务
    public function addTask(){
        
        $post = input('post.');
        if (empty($post["uid"])){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }

        
        $post["create_time"] = time();
        if(empty($post["notice_time"])){
            $post["type"] = 1;
            $taskid = Db::name('task')->insertGetId($post);
        }else{
            $notice_time = $post["notice_time"];
            $post["notice_time"] = implode(",",$post["notice_time"]);
            $post["type"] = 1;
            //unset($post["notice_time"]);
            $taskid = Db::name('task')->insertGetId($post);
            $user = Db::name('onuser')->where("uid",$post["uid"])->select();
            foreach($notice_time as $k=>$v){
                $n_post["uid"] = $post["uid"];
                $n_post["openid"] = $user[0]["openid"];
                $n_post["nick"] = $user[0]["username"];
                $n_post["is_notice"] = 0;
                $n_post["notice"] = (int)$v;
                $n_post["task_id"] = $taskid;
                Db::name("notice")->insert($n_post);
                //var_dump($n_post);

        }

        }
        
        return json(['code'=>($this->code[0]),'msg'=>'添加任务成功','data'=>["taskid" => $taskid]]);
        
    }

    //获取任务信息
    public function getTaskInfo(){
        $uid = input("uid");
        $taskid = input("taskid");
         if ($uid == null){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        if ($taskid == null){
            return json(['code'=>($this->code[2]),'msg'=>'未携带任务id','data'=>null]);
        }
        $task = Db::name('task')->where(["id"=>$taskid,"type"=>1])->select();
        //var_dump($task[0]["notice_time"]);
        if($task!=null){
            $task["notice_time"]  = explode(",",$task[0]["notice_time"]);
        }

        return json(['code'=>($this->code[0]),'msg'=>'任务获取成功','data'=>["task" => $task]]);

    }

    //获取用户全部任务列表
    public function getTask(){
        $uid = input("uid");
        if($uid==null){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        $task = Db::name('task')->where(["uid"=>$uid,"type"=>1])->select();
        return json(['code'=>($this->code[0]),'msg'=>'获取任务列表成功','data'=>["task" => $task]]); 

    }
    //删除指定任务
    public function deleteTask(){
        $uid = input("uid");
        $taskid = input("taskid");
         if ($uid == null){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        if ($taskid == null){
            return json(['code'=>($this->code[2]),'msg'=>'未携带任务id','data'=>null]);
        }
        $task = Db::name('task')->where(["id"=>$taskid,"type"=>1])->delete();
        if($task){
            return json(['code'=>($this->code[0]),'msg'=>'任务删除成功']);
        }else{
            return json(['code'=>($this->code[3]),'msg'=>'无效任务id']);
        }

        


    }

   



    //获取用户已打卡任务列表
    public function getEndTask(){
        $uid = input("uid");
        if($uid==null){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        $task = Db::name('task')->where(["uid"=>$uid,"is_end"=>1,"type"=>1])->select();
        return json(['code'=>($this->code[0]),'msg'=>'获取任务列表成功','data'=>["task" => $task]]); 

    }
    //获取用户未打卡任务列表
    public function getOnTask(){
        $uid = input("uid");
        if($uid==null){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        $task = Db::name('task')->where(["uid"=>$uid,"is_end"=>0,"type"=>1])->select();
        return json(['code'=>($this->code[0]),'msg'=>'获取任务列表成功','data'=>["task" => $task]]); 

    }


    //打卡成功，修改is_end
    public function taskEnd(){
        $uid = input("uid");
        $taskid = input("taskid");

        if (empty($uid)){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        if (empty($taskid)){
            return json(['code'=>($this->code[4]),'msg'=>'未携带任务id','data'=>null]);
        }
        if(Db::name('task')
                  ->where("id",$taskid)
                  ->update([
                          'is_end'=>1
                  ])){
            return json(['code'=>($this->code[0]),'msg'=>'打卡成功']);
        }else{
            return json(['code'=>($this->code[3]),'msg'=>'已打卡，请勿重复打卡']);
        }

    }

    //更新任务信息
    public function updateTask(){
        $request = Request::instance();
        $post = $request->post();
        if (empty($post["uid"])){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        if (empty($post["taskid"])){
            return json(['code'=>($this->code[4]),'msg'=>'未携带任务id','data'=>null]);
        }
        $notice_time = $post["notice_time"];
        $uid = $post["uid"];
        $taskid = $post["taskid"];
        $post["notice_time"] = implode(",",$post["notice_time"]);
         unset($post["uid"]);
         unset($post["taskid"]);
        //->where(["uid"=>$post["uid"],"id"=>$post["taskid"]])
        if (DB::name("task")->where(["uid"=>$uid,"id"=>$taskid])->update($post)){
            Db::name("notice")->where("task_id",$taskid)->delete();
            $user = Db::name('onuser')->where("uid",$uid)->select();
            foreach($notice_time as $k=>$v){
                $n_post["uid"] = $uid;
                $n_post["openid"] = $user[0]["openid"];
                $n_post["nick"] = $user[0]["username"];
                $n_post["is_notice"] = 0;
                $n_post["notice"] = (int)$v;
                $n_post["task_id"] = $taskid;
                Db::name("notice")->insert($n_post);
                //var_dump($n_post);

            }
            return json(['code'=>($this->code[0]),'msg'=>'更新活动成功']);
        }else{
            return json(['code'=>($this->code[3]),'msg'=>'更新活动失败，请勿重复更新']);
        }
    }

    //随手记接口

    //添加任务
    public function addNote(){
        
        $post = input('post.');
        if (empty($post["uid"])){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }

        
        $post["create_time"] = time();
        $post["type"] = 2;
        
        $noteid = Db::name('task')->insertGetId($post);
         
        return json(['code'=>($this->code[0]),'msg'=>'添加随手记成功','data'=>["noteid" => $noteid]]);
        
    }
     //获取指定任务的信息
    public function getNoteInfo(){
        $uid = input("uid");
        $taskid = input("noteid");
         if ($uid == null){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        if ($taskid == null){
            return json(['code'=>($this->code[2]),'msg'=>'未携带随手记id','data'=>null]);
        }
        $note= Db::name('task')->field('id,uid,title,text,create_time')->where(["id"=>$taskid,"type"=>2])->select();


        return json(['code'=>($this->code[0]),'msg'=>'随手记获取成功','data'=>["note" => $note]]);

    }

    
    //获取用户的所有随手记信息
    public function getNote(){
        $uid = input("uid");
        if($uid==null){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        $note = Db::name('task')->field('id,uid,title,text,create_time')->where(["uid"=>$uid,"type"=>2])->select();
        return json(['code'=>($this->code[0]),'msg'=>'获取随手记列表成功','data'=>["note" => $note]]); 

    }

    //更新随手记信息
    public function updateNote(){
        $request = Request::instance();
        $post = $request->post();
        if (empty($post["uid"])){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        if (empty($post["noteid"])){
            return json(['code'=>($this->code[4]),'msg'=>'未携带随手记id','data'=>null]);
        }
        $uid = $post["uid"];
        $taskid = $post["noteid"];
         unset($post["uid"]);
         unset($post["noteid"]);
        //->where(["uid"=>$post["uid"],"id"=>$post["taskid"]])
        if (DB::name("task")->where(["uid"=>$uid,"id"=>$taskid])->update($post)){
            return json(['code'=>($this->code[0]),'msg'=>'更新随手记成功','data'=>["noteid" => $taskid]]);
        }else{
            return json(['code'=>($this->code[3]),'msg'=>'更新随手记失败，请勿重复更新']);
        }
    }

    //删除指定随手记
    public function deleteNote(){
        $uid = input("uid");
        $noteid = input("noteid");
         if ($uid == null){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        if ($noteid == null){
            return json(['code'=>($this->code[2]),'msg'=>'未携带随手记id','data'=>null]);
        }
        $task = Db::name('task')->where(["id"=>$noteid,"type"=>2])->delete();
        if($task){
            return json(['code'=>($this->code[0]),'msg'=>'随手记删除成功']);
        }else{
            return json(['code'=>($this->code[3]),'msg'=>'无效随手记id']);
        }

        


    }




    /*用户相关接口*/
    //获取用户信息
    public function getUserInfo(){
        $request = Request::instance();
        $uid = $request->get("uid");
        if (empty($uid)){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        $user = Db::name('onuser')->where("uid",$uid)->select();
        return json(['code'=>($this->code[0]),'msg'=>'获取用户信息成功','data'=>["userinfo" => $user]]); 

    }

    //获取所有用户
    public function getAllUserInfo(){
        $user = Db::name('onuser')->select();
        return json(['code'=>($this->code[0]),'msg'=>'获取用户信息成功','data'=>["userinfo" => $user]]); 

    }

    //更新任务信息
    public function updateUser(){
        $request = Request::instance();
        $post = $request->post();
        if (empty($post["uid"])){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        }
        if (DB::name("onuser")->where("uid",$post["uid"])->update($post)){
            return json(['code'=>($this->code[0]),'msg'=>'更新用户成功']);
        }else{
            return json(['code'=>($this->code[3]),'msg'=>'更新用户失败']);
        }


        // $post = $request->post();
        // if (empty($post["uid"])){
        //     return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
        // }
        // $uid = $post["uid"];
        // unset($post["uid"]);
        // if (DB::name("onuser")->where("uid",$uid)->update($post)){
        //     return json(['code'=>($this->code[0]),'msg'=>'更新用户成功']);
        // }else{
        //     return json(['code'=>($this->code[3]),'msg'=>'更新用户失败']);
        // }
    }

     public function upHead()//上传头像接口
    {
        try{
            $request = Request::instance();
            if (empty($request->post("uid"))){
            return json(['code'=>($this->code[2]),'msg'=>'未携带用户uid','data'=>null]);
            }
        
            if($file = $request->file('file')) {
                $data['uid'] = $request->post("uid");
                //$file = request()->file('file');
                $info = $file->validate(['size' => 5242880, 'ext' => 'jpg,png,gif,bmp'])->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info) {
                    $data['avatarUrl'] = $request->root(true) . "/uploads/" . $info->getSaveName();
                    if (Db::name("onuser")->where("uid",$data['uid'])->update(['avatar'=>$data['avatarUrl']]))
                        return json(['code'=>($this->code[0]),'msg'=>'上传成功','data'=>$data['avatarUrl']]);
                }
            }
             return json(['code'=>($this->code[3]),'msg'=>'上传失败','data'=>$file]);
        }
        catch(Exception $e)
        {
            return json(['code'=>($this->code[3]),'msg'=>'上传失败'.$e->getMessage(), 'data' => null]); // 上传失败获取错误信息
        }

    }

    function test(){
        DB::name("task")->where("id",38)->update([
                      'is_end'=>1
              ]);
    }


    

}
